#!/bin/bash
# Startup script for Ubuntu_1804 IPV4 #

echo "app_instance_id_key=($$APP_INSTANCE_ID)" >> /home/init.txt

function prefix() {
declare -A dic
dic=([255]=8 [254]=7 [252]=6 [248]=5 [240]=4 [224]=3 [192]=2 [128]=1 [0]=0)
count=0
a=$((echo "$1" | awk -F "." '{print $1" "$2" "$3" "$4}'))
for num in $a;do
(((count=$count+${dic[$num]})))
done
return $count
}

rm -f /etc/netplan/*.yaml
echo "network:" >> /etc/netplan/50-cloud-init.yaml
echo "    version: 2" >> /etc/netplan/50-cloud-init.yaml
echo "    ethernets:" >> /etc/netplan/50-cloud-init.yaml

ip a | grep '^2:' | awk '{print "        "$2}' >> /etc/netplan/50-cloud-init.yaml
prefix $APP_Public_MASK$
echo "            addresses: [$APP_Public_IP$/$?]" >> /etc/netplan/50-cloud-init.yaml
echo "            gateway4: $APP_Public_GW$" >> /etc/netplan/50-cloud-init.yaml
echo "            mtu: 1500" >> /etc/netplan/50-cloud-init.yaml

ip a | grep '^3:' | awk '{print "        "$2}' >> /etc/netplan/50-cloud-init.yaml
prefix $APP_Private_MASK$
echo "            addresses: [$APP_Private_IP$/$?]" >> /etc/netplan/50-cloud-init.yaml
echo "            routes:" >> /etc/netplan/50-cloud-init.yaml
echo "                 - to: $UE_IP_SEGMENT$" >> /etc/netplan/50-cloud-init.yaml
echo "                   via: $APP_Private_GW$" >> /etc/netplan/50-cloud-init.yaml
echo "                   metric: 50" >> /etc/netplan/50-cloud-init.yaml
echo "                 - to: $ENTERPRISE_CAMPUS$" >> /etc/netplan/50-cloud-init.yaml
echo "                   via: $APP_Private_GW$" >> /etc/netplan/50-cloud-init.yaml
echo "                   metric: 50" >> /etc/netplan/50-cloud-init.yaml
echo "            mtu: 1500" >> /etc/netplan/50-cloud-init.yaml

ip a | grep '^4:' | awk '{print "        "$2}' >> /etc/netplan/50-cloud-init.yaml
prefix $APP_MP1_MASK$
echo "            addresses: [$APP_MP1_IP$/$?]" >> /etc/netplan/50-cloud-init.yaml
echo "            mtu: 1500" >> /etc/netplan/50-cloud-init.yaml

netplan apply